home *** CD-ROM | disk | FTP | other *** search
/ MacHack 2001 / MacHack 2001.toast / pc / The Hacks / CDaemon / HACK16 / Sources / MyAppleEvents.c < prev    next >
Encoding:
Text File  |  2001-06-23  |  4.4 KB  |  175 lines

  1. /*
  2.     File:        FBAAppleEvents.c
  3.  
  4.     Contains:    
  5.  
  6.     Written by: Greg Sutton    
  7.  
  8.     Copyright:    Copyright © 1996-1999 by Apple Computer, Inc., All Rights Reserved.
  9.  
  10.                 You may incorporate this Apple sample source code into your program(s) without
  11.                 restriction. This Apple sample source code has been provided "AS IS" and the
  12.                 responsibility for its operation is yours. You are not permitted to redistribute
  13.                 this Apple sample source code as "Apple sample source code" after having made
  14.                 changes. If you're going to re-distribute the source, we require that you make
  15.                 it clear in the source that the code was descended from Apple sample source
  16.                 code, but that you've made changes.
  17.  
  18.     Change History (most recent first):
  19.                 7/21/1999    Karl Groethe    Updated for Metrowerks Codewarror Pro 2.1
  20.                 
  21.  
  22. */
  23.  
  24. //    This has been modfied a bit by me -Shawn Platkus
  25.  
  26. #include "MyAppleEvents.h"
  27.  
  28. #include "MyIncludes.h"
  29.  
  30. #include <Events.h>
  31. #include <Errors.h>
  32. #include <AERegistry.h>
  33. #include <Gestalt.h>
  34. #include <Resources.h>
  35.  
  36. // prototypes
  37. static pascal OSErr HandleOpenApplication( AppleEvent *theAppleEvent, AppleEvent *theReply, long theRefcon );
  38. static pascal OSErr HandleQuitApplication( AppleEvent *theAppleEvent, AppleEvent *theReply, long theRefcon );
  39. static pascal OSErr HandleOpenDocuments( AppleEvent *theAppleEvent, AppleEvent *theReply, long theRefcon );
  40. static pascal OSErr HandlePrintDocuments( AppleEvent *theAppleEvent, AppleEvent *theReply, long theRefcon );
  41.  
  42. static OSErr        HandleOpenDesc( AEDesc* theDesc );
  43. static OSErr        GetDescriptorData( const AEDesc* theDesc, Ptr destPtr, Size maxSize );
  44.  
  45. static OSErr        GetTargetAppDesc( OSType theAppCreator, AEDesc* theTarget );
  46.  
  47.  
  48. // Globals
  49. extern    bool        gbQuit;
  50.  
  51.  
  52. OSErr InitAppleEvents ( void )
  53. {
  54.     short err;
  55.     long response;
  56.     
  57.     err = Gestalt( gestaltAppleEventsAttr, &response );
  58.     if (noErr != err)
  59.         response = 0L;
  60.  
  61.     if ( ! ( response & (1 << gestaltAppleEventsPresent)) )
  62.         return gestaltUndefSelectorErr;
  63.     
  64.     err = AEInstallEventHandler ( kCoreEventClass, kAEOpenApplication,
  65.                         NewAEEventHandlerProc(HandleOpenApplication), 0L, false );
  66.     if (noErr != err) goto done;
  67.  
  68.     err = AEInstallEventHandler ( kCoreEventClass, kAEQuitApplication,
  69.                         NewAEEventHandlerProc(HandleQuitApplication), 0L, false );
  70.     if (noErr != err) goto done;
  71.  
  72.     err = AEInstallEventHandler ( kCoreEventClass, kAEOpenDocuments,
  73.                         NewAEEventHandlerProc(HandleOpenDocuments), 0L, false );
  74.     if (noErr != err) goto done;
  75.  
  76.     err = AEInstallEventHandler ( kCoreEventClass, kAEPrintDocuments,
  77.                         NewAEEventHandlerProc(HandlePrintDocuments), 0L, false );
  78.  
  79. done:        
  80.     return err;
  81. }
  82.  
  83.  
  84.  
  85. OSErr DoAppleEvent( EventRecord *event )
  86. {
  87.     short err = noErr;
  88.     
  89.     err = AEProcessAppleEvent ( event );
  90.     return err;
  91. }
  92.  
  93.  
  94.  
  95. static pascal OSErr HandleOpenApplication( AppleEvent *theAppleEvent, AppleEvent *theReply, long theRefcon )
  96. {
  97. #ifdef __MWERKS__
  98.     #pragma unused (theAppleEvent, theReply, theRefcon )
  99. #endif
  100.     // don't do anything here
  101.     return noErr;
  102. }
  103.  
  104.  
  105.  
  106. static pascal OSErr HandleQuitApplication( AppleEvent *theAppleEvent, AppleEvent *theReply, long theRefcon )
  107. {
  108. #ifdef __MWERKS__
  109.     #pragma unused (theAppleEvent, theReply, theRefcon )
  110. #endif
  111.  
  112.     gbQuit = true;
  113.     return noErr;
  114. }
  115.  
  116.  
  117. // If we get a folder dropped onto the application then we'll add it to our
  118. // list of folders to watch.
  119.  
  120. static pascal OSErr HandleOpenDocuments( AppleEvent *theAppleEvent, AppleEvent *theReply, long theRefcon )
  121. {
  122. #ifdef __MWERKS__
  123.     #pragma unused( theAppleEvent, theReply, theRefcon )
  124. #endif
  125.  
  126. // do nothing here
  127.     return noErr;
  128. }
  129.  
  130.  
  131. static OSErr    HandleOpenDesc( AEDesc* theDesc )
  132. {
  133. #ifdef __MWERKS__
  134.     #pragma unused(theDesc)
  135. #endif
  136.  
  137. // do nothing here
  138.     return noErr;
  139. }
  140.  
  141.  
  142. static OSErr    GetDescriptorData( const AEDesc* theDesc, Ptr destPtr, Size maxSize )
  143. {
  144.     Size    copySize;
  145.     OSErr    err;
  146.     
  147.     if ( typeNull == theDesc->descriptorType || ! theDesc->dataHandle )
  148.         return( errAENotAEDesc );
  149.  
  150.     copySize = GetHandleSize( (Handle)theDesc->dataHandle );
  151.     if ( copySize <= maxSize )
  152.     {
  153.         HLock( (Handle)theDesc->dataHandle );
  154.         BlockMoveData( *theDesc->dataHandle, destPtr, copySize );
  155.         HUnlock( (Handle)theDesc->dataHandle );
  156.     }
  157.     else
  158.         err = errAECorruptData;
  159.     
  160.  
  161.     return noErr;
  162. } // GetDescriptorData
  163.  
  164.  
  165.  
  166. static pascal OSErr HandlePrintDocuments( AppleEvent *theAppleEvent, AppleEvent *theReply, long theRefcon )
  167. {
  168. #ifdef __MWERKS__
  169.     #pragma unused(theAppleEvent, theReply, theRefcon )
  170. #endif
  171.  
  172.     // Faceless Background Applications can't print documents
  173.     return errAEEventNotHandled;
  174. }
  175.